{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "BasicBlock Net\n",
    "\n",
    "https://github.com/Lornatang/GoogLeNet-PyTorch/blob/main/model.py\n",
    "为了提升网络的表达能力同时控制计算成本，Szegedy等人在2014年提出了InceptionNet（Szegedy et al., 2015），该网络在ImageNet图像分类任务中取得了优异成绩。InceptionNet的核心思想是：在每个模块中并行使用多个不同尺寸的卷积核（如1×1、3×3、5×5），以捕捉不同尺度的特征信息。\n",
    "\n",
    "![alt text](<../../image_src/inception module.jpg>)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class BasicConv2d(nn.Module):\n",
    "    def __init__(self, in_channels: int, out_channels: int, **kwargs) -> None:\n",
    "        super(BasicConv2d, self).__init__()\n",
    "        self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)\n",
    "        self.bn = nn.BatchNorm2d(out_channels, eps=0.001)\n",
    "        self.relu = nn.ReLU(True)\n",
    "\n",
    "    def forward(self, x):\n",
    "        out = self.conv(x)\n",
    "        out = self.bn(out)\n",
    "        out = self.relu(out)\n",
    "\n",
    "        return out\n",
    "\n",
    "\n",
    "class BasicBlock(nn.Module):\n",
    "    def __init__(\n",
    "            self,\n",
    "            in_channels: int,\n",
    "            ch1x1: int,\n",
    "            ch3x3red: int,\n",
    "            ch3x3: int,\n",
    "            ch5x5red: int,\n",
    "            ch5x5: int,\n",
    "            pool_proj: int,\n",
    "    ) -> None:\n",
    "        super(BasicBlock, self).__init__()\n",
    "        self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=(1, 1), stride=(1, 1), padding=(0, 0))\n",
    "\n",
    "        self.branch2 = nn.Sequential(\n",
    "            BasicConv2d(in_channels, ch3x3red, kernel_size=(1, 1), stride=(1, 1), padding=(0, 0)),\n",
    "            BasicConv2d(ch3x3red, ch3x3, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n",
    "        )\n",
    "\n",
    "        self.branch3 = nn.Sequential(\n",
    "            BasicConv2d(in_channels, ch5x5red, kernel_size=(1, 1), stride=(1, 1), padding=(0, 0)),\n",
    "            BasicConv2d(ch5x5red, ch5x5, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),\n",
    "        )\n",
    "\n",
    "        self.branch4 = nn.Sequential(\n",
    "            nn.MaxPool2d(kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), ceil_mode=True),\n",
    "            BasicConv2d(in_channels, pool_proj, kernel_size=(1, 1), stride=(1, 1), padding=(0, 0)),\n",
    "        )\n",
    "\n",
    "    def forward(self, x):\n",
    "        branch1 = self.branch1(x)\n",
    "        branch2 = self.branch2(x)\n",
    "        branch3 = self.branch3(x)\n",
    "        branch4 = self.branch4(x)\n",
    "        out = [branch1, branch2, branch3, branch4]\n",
    "\n",
    "        out = torch.cat(out, 1)\n",
    "\n",
    "        return out\n",
    "\n",
    "\n",
    "class GoogLeNet(nn.Module):\n",
    "    def __init__(\n",
    "            self,\n",
    "            in_channels: int = 1,\n",
    "            num_classes: int = 1000,\n",
    "            transform_input: bool = False,\n",
    "            dropout: float = 0.2,\n",
    "\n",
    "    ) -> None:\n",
    "        super(GoogLeNet, self).__init__()\n",
    "        self.transform_input = transform_input\n",
    "\n",
    "        self.conv1 = BasicConv2d(in_channels, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))\n",
    "        self.maxpool1 = nn.MaxPool2d((3, 3), (2, 2), ceil_mode=True)\n",
    "        self.conv2 = BasicConv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), padding=(0, 0))\n",
    "        self.conv3 = BasicConv2d(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
    "        self.maxpool2 = nn.MaxPool2d((3, 3), (2, 2), ceil_mode=True)\n",
    "\n",
    "        self.inception3a = BasicBlock(192, 64, 96, 128, 16, 32, 32)\n",
    "        self.inception3b = BasicBlock(256, 128, 128, 192, 32, 96, 64)\n",
    "        self.maxpool3 = nn.MaxPool2d((3, 3), (2, 2), ceil_mode=True)\n",
    "\n",
    "        self.inception4a = BasicBlock(480, 192, 96, 208, 16, 48, 64)\n",
    "        self.inception4b = BasicBlock(512, 160, 112, 224, 24, 64, 64)\n",
    "        self.inception4c = BasicBlock(512, 128, 128, 256, 24, 64, 64)\n",
    "        self.inception4d = BasicBlock(512, 112, 144, 288, 32, 64, 64)\n",
    "        self.inception4e = BasicBlock(528, 256, 160, 320, 32, 128, 128)\n",
    "        self.maxpool4 = nn.MaxPool2d((2, 2), (2, 2), ceil_mode=True)\n",
    "\n",
    "        self.inception5a = BasicBlock(832, 256, 160, 320, 32, 128, 128)\n",
    "        self.inception5b = BasicBlock(832, 384, 192, 384, 48, 128, 128)\n",
    "\n",
    "        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n",
    "        self.dropout = nn.Dropout(dropout, True)\n",
    "        self.fc = nn.Linear(1024, num_classes)\n",
    "\n",
    "        # Initialize neural network weights\n",
    "        self._initialize_weights()\n",
    "        \n",
    "    def _initialize_weights(self) -> None:\n",
    "        for module in self.modules():\n",
    "            if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):\n",
    "                torch.nn.init.trunc_normal_(module.weight, mean=0.0, std=0.01, a=-2, b=2)\n",
    "            elif isinstance(module, nn.BatchNorm2d):\n",
    "                nn.init.constant_(module.weight, 1)\n",
    "                nn.init.constant_(module.bias, 0)\n",
    "                \n",
    "    def forward(self, x):\n",
    "        out = self.conv1(x)\n",
    "        out = self.maxpool1(out)\n",
    "        out = self.conv2(out)\n",
    "        out = self.conv3(out)\n",
    "        out = self.maxpool2(out)\n",
    "\n",
    "        out = self.inception3a(out)\n",
    "        out = self.inception3b(out)\n",
    "        out = self.maxpool3(out)\n",
    "        out = self.inception4a(out)\n",
    "\n",
    "        out = self.inception4b(out)\n",
    "        out = self.inception4c(out)\n",
    "        out = self.inception4d(out)\n",
    "\n",
    "        out = self.inception4e(out)\n",
    "        out = self.maxpool4(out)\n",
    "        out = self.inception5a(out)\n",
    "        out = self.inception5b(out)\n",
    "\n",
    "        out = self.avgpool(out)\n",
    "        out = torch.flatten(out, 1)\n",
    "        out = self.dropout(out)\n",
    "        out = self.fc(out)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inps = torch.randn((2, 1, 128, 128))\n",
    "\n",
    "model = GoogLeNet(\n",
    "        in_channels=1,\n",
    "        num_classes = 1000,\n",
    "        transform_input = False,\n",
    "        dropout = 0.2,\n",
    ")\n",
    "\n",
    "out = model(inps)\n",
    "out.size()"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
